package com.navi.loaddata.loadDao;

import com.navi.loaddata.core.errorLog.ErrorCode;
import com.navi.loaddata.core.errorLog.ErrorLog;
import com.navi.loaddata.core.etlException.ETLException2;
import com.navi.loaddata.core.mysqlEntity.EtlConfDModel;
import com.navi.loaddata.core.oracleEntity.*;
import com.navi.loaddata.core.repository.impl.OracleRepositoryImpl;
import com.navi.loaddata.core.util.LoggerUtil;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author xufan
 */
@Service
@Transactional(transactionManager = "thirdaryPlatformTransactionManager",
        rollbackFor = {RuntimeException.class, Exception.class})
public class SaveGlassHstDataDao {
    private static final Logger logger = LoggerFactory.getLogger(SaveGlassHstDataDao.class);
    private LoggerUtil loggerUtil = new LoggerUtil(this.getClass());

    @Autowired
    private OracleRepositoryImpl dao3;

    public void saveGlassHstData(EtlConfDModel etlConfD, EtlGlassHstV etlGlassHstV) throws Exception {
        Session session = dao3.getEntityManagerSession();
        String SQLFileProcedure = "";
        try {
            switch (etlConfD.getShop()) {
                case "ARRAY":
                    SQLFileProcedure = "{Call ARRAY_MES_LOADER.LOAD_GLASS_HST()}";
                    dao3.save(EtlGlassHstVMapper.INSTANCE.etlGlassHstToArray(etlGlassHstV));
                    break;
                case "CF":
                    SQLFileProcedure = "{Call CF_MES_LOADER.LOAD_GLASS_HST()}";
                    dao3.save(EtlGlassHstVMapper.INSTANCE.etlGlassHstToCf(etlGlassHstV));
                    break;
                case "OC":
                    if ("CELL1".equals(etlConfD.getCellType())) {
                        SQLFileProcedure = "{Call CELL1_MES_LOADER.LOAD_COMPONENT_HST()}";
                        dao3.save(EtlGlassHstVMapper.INSTANCE.etlGlassHstToCell1(etlGlassHstV));
                        } else if ("CELL2".equals(etlConfD.getCellType())) {
                        SQLFileProcedure = "{Call CELL2_MES_LOADER.LOAD_CHIP_HST()}";
                        dao3.save(EtlGlassHstVMapper.INSTANCE.etlGlassHstToCell2(etlGlassHstV));
                    }
                    break;
            }
        } catch (Exception e) {
            throw new ETLException2(ErrorCode.ERROR_SAVE_ORA_GLASS_HST,e);
        }


//         call proc
        try {
            SQLQuery queryDefect = session.createSQLQuery(SQLFileProcedure);
            queryDefect.executeUpdate();
        } catch (Exception e) {
            throw new ETLException2(ErrorCode.ERROR_PROCEDURE_ORA_GLASS_HST,e);
        }

    }
    public void deleteGlassHst(EtlConfDModel etlConfD) throws Exception {
        Session session = dao3.getEntityManagerSession();
        String hqlGlassHst = "";
        try {
            switch (etlConfD.getShop()) {
                case "ARRAY":
                    hqlGlassHst = "Delete FROM EtlArrayGlassHstV";
                    break;
                case "CF":
                    hqlGlassHst = "Delete FROM EtlCfGlassHstV";
                    break;
                case "OC":
                    if ("CELL1".equals(etlConfD.getCellType())) {
                        hqlGlassHst = "Delete FROM EtlCellComponentHstV";
                    } else if ("CELL2".equals(etlConfD.getCellType())) {
                        hqlGlassHst = "Delete FROM EtlCell2ChipHstV";
                    }
                    break;
            }
            Query qGlass = session.createQuery(hqlGlassHst);
            qGlass.executeUpdate();


        } catch (Exception e) {
            throw new ETLException2(ErrorCode.ERROR_DELETE_ORA_GLASS_HST,e);
        }
    }




}
